package com.example.tally;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.tally.adapter.AccountAdapter;
import com.example.tally.db.AccountBean;
import com.example.tally.db.DBManager;
import com.example.tally.utils.CalendarDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class HistoryActivity extends AppCompatActivity implements View.OnClickListener {
    ListView historyLv;
    TextView timeTv;
    List<AccountBean> mData;
    AccountAdapter adapter;
    int year , month;

    /**记录点击之后年份和月份的位置，否则每次返回再打开的时候总是刚开始的位置*/
    int dialogSelectPos = -1;
    int dialogSelectMonth = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        historyLv = findViewById(R.id.history_lv);
        timeTv = findViewById(R.id.history_tv_time);
        /**适配器前或者后，先尝试前面——>失败：闪退*/
        initTime();
//        loadData();
        timeTv.setText(year + "年" +month + "月");
        mData = new ArrayList<>();
        // 设置适配器
        adapter = new AccountAdapter(this , mData);
        historyLv.setAdapter(adapter);
        /**适配器前或者后，尝试后面——>成功*/
//        initTime();
        loadData(year , month);
        setListViewLongOnClickListener();
    }

    private void setListViewLongOnClickListener() {
        historyLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                AccountBean bean = mData.get(position);
                delectItem(bean);
                return false;
            }
        });
    }

    private void delectItem(final AccountBean bean) {
        final int deleci_id = bean.getId();
        new AlertDialog.Builder(this).setTitle("提示信息")
                .setMessage("您确定要删除这个记录吗？")
                .setNegativeButton("取消" , null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DBManager.deleteItemFromAccounttbById(deleci_id);
                        mData.remove(bean);// 实时刷新，从数据源删除
                        adapter.notifyDataSetChanged();
                    }
                }).show();
    }

    private void loadData(int year , int month) {
        List<AccountBean> list = DBManager.getAccountListOneMonthFromAccounttb(year, month);
        mData.clear();
        mData.addAll(list);
        adapter.notifyDataSetChanged();
    }

    private void initTime() {
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.history_iv_back:
                finish();
                break;
            case R.id.history_iv_rili:
                CalendarDialog dialog = new CalendarDialog(this , dialogSelectPos , dialogSelectMonth);
                dialog.show();
                dialog.setDialogSize();
                dialog.setOnRefreshListener(new CalendarDialog.OnRefreshListener() {
                    @Override
                    public void Ensure(int selPos, int year, int month) {
                        timeTv.setText(year + "年" + month + "月");
                        loadData(year , month);
                        dialogSelectPos = selPos;
                        dialogSelectMonth = month;
                    }
                });
                break;
        }
    }
}